<?php 
/*
    This file is part of EnvoiMoinsCher's shipping plugin for Joomla VirtueMart.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

   @author Boxtale SAS <contact@envoimoinscher.com>
   @copyright  2011 Boxtale SAS
   @license    http://www.gnu.org/licenses  GNU Lesser General Public License (LGPL)
*/
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); 
 
require_once(dirname(__FILE__)."/../classes/shipping/envoimoinscher.cfg.php");
require_once(dirname(__FILE__)."/../classes/shipping/envoimoinscher.php");
require_once(dirname(__FILE__)."/../classes/shipping/envoimoinscher/WebService.php");
require_once(dirname(__FILE__)."/../classes/shipping/envoimoinscher/ParcelPoint.php"); 

$env = new envoimoinscher;
$host = $env->getHost(envs);
if($host == '')
{
  $host = "https://www.envoimoinscher.com/";
}

if($_GET['popup'] == 1)
{
  echo makeMap('', false);
  $onClick = 'onclick="window.opener.putParcelPoint(\'{CODE}\', \'{NAME}\');"';
}
$poiCl = new Env_ParcelPoint(array("user" => logemc, "pass" => mdpemc, "key" => apiemc));
$poiCl->server = $host;
$poiCl->constructList = true;
$pointsList = explode('|', $_GET['points']);
foreach($pointsList as $point)
{
  // accept only one pattern XXX-alphanumerical (where XXXX corresponds to operator code and alphanumerical to 
  // parcel point id)
  if(preg_match('/^[A-Z]{4}-([A-Za-z0-9]+)$/',$point))
  {
    $poiCl->getParcelPoint("dropoff_point", $point);
  } 
}
?> 
<style type="text/css">
.listPoints {list-style: none;}
</style>
<ul class="listPoints">
  <li><b>Veuillez choisir le point de retrait de votre colis. Vous pouvez visualiser les Relais Colis&reg; sur <a href="#" id="openMap" onclick="javascript:makeMap();return false;">cette carte</a></b></li>
  <?php foreach($poiCl->points['dropoff_point'] as $p => $point) { ?>
  <?php if($point['code'] != '') {  
    $from = array('{CODE}', '{NAME}'); 
    $to = array($point['code'], '<b>'.$point['name'].'</b>');
  ?>
    <li>- <input type="radio" id="point<?php echo $point['code'];?>" name="point" value="<?php echo $point['code'];?>" <?php echo str_replace($from, $to, $onClick);?> />  <b><?php echo $point['name'];?></b> <small>(<?php echo $point['address'];?>, <?php echo $point['zipcode'];?> <?php echo $point['city'];?>)</small>
    <?php $addresses[] = $point['address'].', '.$point['zipcode'].' '.$point['city'];
    $day = array();
    foreach($point['schedule'] as $s => $schedule) 
	{
      $dayArr = setDay($schedule);
      $fromTo = array();
      foreach((array)$dayArr['hours'] as $hour) 
      { 
        $fromTo[] = 'de '.$hour['from'].' &agrave; '.$hour['to'].'';
      }
      $day[] = '<b>'.$dayArr['day'].'</b> <br />'.implode('<br />', $fromTo);
	}
    $infos[] = implode('<br />', $day);
    $names[] = $point['name'];
    $ids[] = "point".$point['code'];
    ?>
    </li>
  <?php } ?>
<?php } ?>
</ul>
<input type="hidden" name="parcelPoints" id="parcelPoints" value="<?php echo implode('|', $addresses);?>" />
<input type="hidden" name="parcelInfos" id="parcelInfos" value="<?php echo implode('|', $infos);?>" />
<input type="hidden" name="parcelNames" id="parcelNames" value="<?php echo implode('|', $names);?>" />
<input type="hidden" name="parcelIds" id="parcelIds" value="<?php echo implode('|', $ids);?>" />   

<?php

function setDay($schedule)
{
  $days = array(1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi',
  6 => 'samedi', 7 => 'dimanche');
  if($schedule['open_am'] != '' && $schedule['close_am'] != '')
  {
    $dispo[] = array('from' => $schedule['open_am'], 'to' => $schedule['close_am']);
  }
  if($schedule['open_pm'] != '' && $schedule['close_pm'] != '')
  {
    $dispo[] = array('from' => $schedule['open_pm'], 'to' => $schedule['close_pm']);
  }
  if(count($dispo) > 0)
  {
    return array('day' => $days[$schedule['weekday']], 'hours' => $dispo);
  }
}
die();